using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraEditors.Mask;
//using System.Text.RegularExpressions;
using System.Globalization;
using ProgramMain.Framework;

namespace ProgramMain.Equipments
{
    public partial class TruncInfoMasksDetailCtl : BaseDetailCtl
    {
        public BindingSource truncInfoDataSource;
        private bool m_bEditMode = false;
        private bool m_bOnActionClose = false;
        public bool m_bEnableChangeType = true;

        public TruncInfoMasksDetailCtl()
        {
            InitializeComponent();
        }

        public DialogResult ShowModal(bool IsEditable)
        {
            this.btnSave.Enabled = IsEditable;

            return frmBaseModal.ShowModal(this, labelCaption.Text, this.btnSave, this.btnCancel);
        }

        override public void FrmCommon_Load(object sender, EventArgs e)
        {
            if (this.ParentForm != null)
            {
                this.ParentForm.FormClosed += new FormClosedEventHandler(this.FrmCommon_FormClosed);
                this.ParentForm.FormClosing += new FormClosingEventHandler(this.FrmCommon_FormClosing);
            }

            DataRowView CurrentRowView = (DataRowView)this.truncInfoDataSource.Current;
            m_bEditMode = CurrentRowView.Row.RowState != DataRowState.Added
                && CurrentRowView.Row.RowState != DataRowState.Detached;

            this.BindDataControls();
        }

        private void BindDataControls()
        {
            this.txtEditMask.DataBindings.Add(new Binding("Text", this.truncInfoDataSource, "Mask"));
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!this.CustomValidate())
            {
                foreach (Control c in dxValidationProvider1.GetInvalidControls())
                {
                    DevExpress.XtraEditors.DXErrorProvider.ValidationRuleBase rule = 
                        dxValidationProvider1.GetValidationRule(c);
                    XtraMessageBox.Show(rule.ErrorText);
                    c.Focus();
                    return;
                }
            }

            m_bOnActionClose = true;

            this.truncInfoDataSource.EndEdit();
            if (this.ParentForm != null)
                this.ParentForm.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            m_bOnActionClose = true;

            if (m_bEditMode)
            {
                this.truncInfoDataSource.CancelEdit();
            }
            else
            {
                this.truncInfoDataSource.RemoveCurrent();
            }
            if (this.ParentForm != null)
                this.ParentForm.Close();
        }

        public void FrmCommon_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!m_bOnActionClose)
            {
                m_bOnActionClose = true;
                if (m_bEditMode)
                {
                    this.truncInfoDataSource.CancelEdit();
                }
                else
                {
                    this.truncInfoDataSource.RemoveCurrent();
                }
            }
        }

        public void FrmCommon_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.ParentForm != null)
            {
                e.Cancel = this.ParentForm.DialogResult != DialogResult.Cancel
                    && !this.CustomValidate();
            }
        }

        private bool CustomValidate()
        {
            bool bRes = dxValidationProvider1.Validate();

            return bRes;
        }
    }
}
